package com.psm.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.psm.controller.BaseController;
import com.psm.controller.IndexController;
import com.psm.model.IdGenerater;
import com.psm.model.SysUser;

import org.apache.poi.ss.formula.functions.Index;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cheam on 15/6/19.
 */
public class IndexInterceptor implements Interceptor {
    protected static IdGenerater idGenerater=new IdGenerater();


    public void intercept(Invocation ai) {
        System.out.println("Before invoking " + ai.getActionKey());
        Controller ctl = ai.getController();

        String login_user_id=ctl.getCookie("login_user_id");

        if(null == login_user_id){
            ctl.redirect("/admin/login.html");
        }else{
            String path = ctl.getRequest().getContextPath();
            String basePath = ctl.getRequest().getScheme() + "://" + ctl.getRequest().getServerName() + ":" + ctl.getRequest().getServerPort() + path + "/";

            ctl.setAttr("root",basePath);

            if(null == ctl.getSessionAttr("permission_button")){
                ctl.setSessionAttr("permission_button", SysUser.me.get_menu_permission(idGenerater.to10(login_user_id)));
            }
            ctl.setAttr("permission_button",ctl.getSessionAttr("permission_button"));
            ai.invoke();
        }
        System.out.println("After invoking " + ai.getActionKey());
    }

}
